package com.scs.application.modules.svn.controller;

import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.svn.dto.SVNLockInfo;
import com.scs.application.modules.svn.dto.SVNOperateDTO;
import com.scs.application.modules.svn.service.SVNService;
import com.scs.application.modules.sys.model.RestResult;
import com.scs.application.modules.sys.properties.SystemProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * SVN 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/program/svn")
public class SVNController {

    @Autowired
    private SystemProperties systemProperties;

    private List<SVNService> svnServices;

    @Autowired
    private void setSVNServices(List<SVNService> svnServices) {
        this.svnServices = svnServices;
    }

    @RequestMapping(value = "/status", method = RequestMethod.GET)
    public RestResult status(SVNOperateDTO para) {
        SVNLockInfo svnLockInfo = getSVNService().status(para);
        String fileName = para.getSourceType().equals("init-data") ? "init-data-sql.json" : para.getFileName();
        return RestResult.ok("获取资源锁定状态成功，文件：" + fileName, svnLockInfo);
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public RestResult add(SVNOperateDTO para) {
        getSVNService().setSourceFile(para.getSourceType(), para.getFileName());
        return RestResult.ok("锁定资源请求完成！", getSVNService().add());
    }

    @RequestMapping(value = "/lock", method = RequestMethod.GET)
    public RestResult lock(SVNOperateDTO para) {
        getSVNService().setSourceFile(para.getSourceType(), para.getFileName());
        return RestResult.ok("锁定资源请求完成！", getSVNService().lock(para));
    }

    @RequestMapping(value = "/commit", method = RequestMethod.GET)
    public RestResult commit(SVNOperateDTO para) {
        getSVNService().setSourceFile(para.getSourceType(), para.getFileName());
        return RestResult.ok("提交资源请求完成！", getSVNService().commit(para));
    }

    @RequestMapping(value = "/revert", method = RequestMethod.GET)
    public RestResult revert(SVNOperateDTO para) {
        getSVNService().setSourceFile(para.getSourceType(), para.getFileName());
        return RestResult.ok("撤销提交资源成功！", getSVNService().revert(para));
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public RestResult delete(SVNOperateDTO para) {
        getSVNService().setSourceFile(para.getSourceType(), para.getFileName());
        return RestResult.ok("删除资源成功！", getSVNService().delete());
    }

    /**
     * @Description： 批量锁定
     */
    @RequestMapping(value = "/lockBatch", method = RequestMethod.GET)
    public RestResult lockBatch(SVNOperateDTO para) {
        final int[] successNum = {0};
        if (StringUtils.isNotBlank(para.getFileNames())) {
            String[] fileNames = para.getFileNames().split(",");
            Arrays.stream(fileNames).forEach(fileName -> {
                getSVNService().setSourceFile(para.getSourceType(), fileName);
                para.setFileName(fileName);
                getSVNService().lock(para);
                successNum[0]++;
            });
        }

        return RestResult.ok("批量锁定成功，总计：" + successNum[0], null);
    }


    /**
     * @Description： 批量解锁
     */
    @RequestMapping(value = "/revertBatch", method = RequestMethod.GET)
    public RestResult revertBatch(SVNOperateDTO para) {
        final int[] successNum = {0};
        if (StringUtils.isNotBlank(para.getFileNames())) {
            String[] fileNames = para.getFileNames().split(",");
            Arrays.stream(fileNames).forEach(fileName -> {

                getSVNService().setSourceFile(para.getSourceType(), fileName);
                para.setFileName(fileName);
                getSVNService().revert(para);
                successNum[0]++;
            });
        }

        return RestResult.ok("批量撤销成功，总计：" + successNum[0], null);
    }


    private SVNService getSVNService() {
        SVNService svnService = null;
        for (SVNService svnService1 : svnServices) {
            if (svnService1.isSupport(systemProperties.getFlagProject())) {
                svnService = svnService1;
                break;
            }
        }
        return svnService;
    }
}
